<!-- 加工统计表 -->
<template>
  <div class="mod-config">
    <basic-container>
      <el-divider content-position="left">查询条件</el-divider>
      <el-form size="mini" label-width="80px">
        <el-form-item label="工号">
          <el-input size="mini" v-model="dataForm.workNumber" style="width: 175px;" @change="getRealName"></el-input>
        </el-form-item>
        <el-form-item label="加工师">
          <el-input size="mini" v-model="dataForm.workUserName" style="width: 175px;" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品品种">
          <el-input v-model="dataForm.brandName" readonly>
            <el-button slot="append" @click="selectBrand">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="查询加工日期">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.workTimeStart" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">至</el-col>
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="dataForm.workTimeEnd" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="销售部门">
          <el-select v-model="dataForm.deptId">
            <el-option v-for="item in deptOptions" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>

        <el-row style="margin-top: 10px;">
          <el-form-item label-width="5px">
            <el-link  :href="printUrl" target="_blank" type="primary"><el-button size="mini" type="primary" @click="getWorkParams">查 询</el-button></el-link>
          </el-form-item>
        </el-row>

      </el-form>
      <search-brand v-if="brandVisible" ref="selectBrand" @refreshData="updateFormBrand"></search-brand>
    </basic-container>
  </div>
</template>

<script>
    let params = ''
    import { filterParams } from '@/util/tools'
    import { getDateFormat } from '@/util/tools'
    import { mapGetters } from 'Vuex'
    import {getDeptInfo} from '@/api/admin/dept'
    import { getUserInfo} from '@/api/admin/user' //根据工号 获取用户信息
    import SearchBrand from './SearchBrand'

    export default {
        data () {
            return {
                printUrl: '',
                dataForm: {
                    workUserName: '',
                    brandName: '',
                    workTimeStart: '',
                    workTimeEnd: '',
                    deptId: ''
                },
                brandVisible: false,
                deptOptions: []
            };
        },
        computed: {
            ...mapGetters(['userInfo','bizData'])
        },
        created() {
            this.dataForm.workTimeStart = getDateFormat(new Date())
            this.dataForm.workTimeEnd = getDateFormat(new Date())
            this.getDeptList();
        },
        components: {
            SearchBrand
        },
        methods:{
            getWorkParams() {
                params = '/processingInfo.cpt'
                let data = filterParams(this.dataForm)
                let parameter = ''
                for(let key in data){
                    parameter += '&' + key + '=' + this.dataForm[key]
                }
                //let parameter = '&deptId='+this.dataForm.deptId + '&beginTime='+this.dataForm.beginTime+ '&endTime='+this.dataForm.endTime
                this.printUrl = this.bizData.printUrl + params + parameter
            },
            //  获取部门列表
            getDeptList() {
                getDeptInfo().then(result => {
                    this.deptOptions = result.data.data
                })
            },
            //根据工号获取员工真实名称
            getRealName(workNumber) {
                if(workNumber == ''){
                    this.dataForm.workUserName = '';
                    this.dataForm.workUserId = '';
                }else{
                    getUserInfo(workNumber).then(result => {
                        if (result.data.data) {
                            this.dataForm.workUserName = result.data.data.realName;
                            this.dataForm.workUserId = result.data.data.id;
                        } else {
                            this.$message.warning("请输入正确的加工师工号");
                        }
                    })
                }
            },
            selectBrand() {
                this.brandVisible = true
                this.$nextTick(() => {
                    this.$refs.selectBrand.init()
                })
            },
            //  更新表单品种信息
            updateFormBrand(data) {
                this.dataForm.brandId = data.id
                this.dataForm.brandName = data.name
                this.dataForm.brandCode = data.code
                this.dataForm.name = data.name
                this.dataForm.unitType = data.unitType
            },
        }
    }
</script>

<style lang="scss" scoped>
  .row-bg {
    background-color: #cadee8;
    // padding-left: 15px;
    padding-right: 15px;
    padding-top: 5px;
    margin-bottom: 2px;
  }
</style>
